package com.yyg.boot.web;

import com.yyg.boot.domain.User;
import com.yyg.boot.service.MyService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/5/9
 * @Description Description
 */
@RestController
public class ClientController {

    /**
     * webservice接口地址
     */
    private static String address = "http://localhost:8080/myService/api?wsdl";

    @GetMapping("/show")
    public String showMsg() {
        try {
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            //添加用户名密码拦截器
            //jaxWsProxyFactoryBean.getOutInterceptors().add(new LoginInterceptor("root","admin"));;
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(MyService.class);
            // 创建一个代理接口实现
            MyService service = (MyService) jaxWsProxyFactoryBean.create();
            // 调用代理接口的方法调用并返回结果
            return service.sayHello("hello");
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @GetMapping("/users")
    public List<User> showUsers() {
        try {
            // 代理工厂
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 设置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 设置接口类型
            jaxWsProxyFactoryBean.setServiceClass(MyService.class);
            // 创建一个代理接口实现
            MyService service = (MyService) jaxWsProxyFactoryBean.create();
            // 调用代理接口的方法调用并返回结果
            return service.getUsers();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
